<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
	Integer patientUid = input.getInt("patient_uid", -1);
	Integer surveyId = input.getInt("survey_id", -1);
	Integer uid = token.getUid();
	User user = userByUid(uid);
	if (user != null) {
		if (USER_PATIENT.equals(user.getType())) 
			patientUid = uid;
	}
	Integer kind = input.getInt("kind", -1);
	if (patientUid != -1 && kind == 7 && user.getType().equals(USER_EMPLOYEE)) {
		if (!validateReportAuth(token, patientUid)) {
			msg.setErrcodeAndErrmsg(30021, "您没有权限查看该患者的病历");
			Tools.encryptPrint(out, msg); 
			return;
		}
	}
	Where where = new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("patient_uid=", patientUid).and("kind=", kind);
	if (surveyId != -1) {
		where.and("oid=", surveyId);
	}
	List<Map> timeLines = QBuilder.db("basedb").select("new map(id as id, type as type, date as date, treat_progress as treat_progress, case when imgs is null then '[]' else imgs end as imgs, case when voices is null then '[]' else voices end as voices, diagnose as diagnose, visit_hospital_name as visit_hospital_name, visit_dept_name as visit_dept_name, visit_doctor_name as visit_doctor_name)").from("time_line").where(where).order("create_time", "desc").query().getResultObj();
	User patient = userByUid(patientUid);
	for (Map map:timeLines) {
		map.put("patient_name", patient.getName()!=null?patient.getName():patient.getNickName()!=null?patient.getNickName():patient.getUserName());
		map.put("imgs", JSONUtil.fromJson(isEmpty(map.get("imgs") + "")?"[]":(map.get("imgs") + ""), new TypeReference<List<String>>(){}));
	}
	msg.put("data", timeLines);
    Tools.encryptPrint(out, msg);
%>
